<template>
<el-dialog :title="!dataForm.id ? '新建' :'编辑'"
           :close-on-click-modal="false" append-to-body
           :visible.sync="visible" class="JNPF-dialog JNPF-dialog_center" lock-scroll
           width="600px">
<el-row :gutter="15" class="">
<el-form ref="elForm" :model="dataForm" :rules="rules" size="small" label-width="100px" label-position="right" >
    <template v-if="!loading">
            <el-col :span="24"  >
                <el-form-item  label="供应商编码"   prop="code" >
                    <el-input   v-model="dataForm.code" placeholder="系统自动生成" :disabled="true" clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="供应商名称"   prop="name" >
                    <el-input   v-model="dataForm.name" placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="是否启用"   prop="enabledmark" >
                    <el-select   v-model="dataForm.enabledmark" placeholder="请选择"  clearable  :style='{"width":"100%"}'>
                            <el-option v-for="(item, index) in enabledmarkOptions" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled" ></el-option>

                </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="单据状态"   prop="status" >
                    <el-select   v-model="dataForm.status" placeholder="请选择" :disabled="true" clearable  :style='{"width":"100%"}'>
                            <el-option v-for="(item, index) in statusOptions" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled" ></el-option>

                </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="附件上传"   prop="attachment" >
                    <JNPF-UploadFz   v-model="dataForm.attachment" :fileSize="10"    :showTip="true"  sizeUnit="MB"  :limit="9"  buttonText="点击上传" >

                </JNPF-UploadFz>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="修改用户"   prop="lastmodifyuserid" >
                    <el-input   v-model="dataForm.lastmodifyuserid" placeholder="系统自动生成"  readonly >

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="修改时间"   prop="lastmodifytime" >
                    <el-input   v-model="dataForm.lastmodifytime" placeholder="系统自动生成"  readonly >

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="修改原因"   prop="modifyreason" >
                    <el-input   v-model="dataForm.modifyreason" placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="修改描述"   prop="description" >
                    <el-input   v-model="dataForm.description" placeholder="请输入"  readonly  :style='{"width":"100%"}' true  type="textarea"  :autosize='{"minRows":4,"maxRows":4}' >

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="审批时间"   prop="approvetime" >
                    <el-date-picker   v-model="dataForm.approvetime" placeholder="请选择" :disabled="true" clearable  :style='{"width":"100%"}' type="datetime"  format="yyyy-MM-dd HH:mm:ss"  value-format="timestamp" >

                </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="审批人"   prop="approver" >
                    <user-select   v-model="dataForm.approver" placeholder="请选择" :disabled="true" clearable >

                </user-select>
                </el-form-item>
            </el-col>
    </template>
</el-form>
    <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm"
              ref="selectDialog" @select="addForSelect" @close="selectDialogVisible=false"/>
    </el-row>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false"> 取 消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 确 定</el-button>
    </span>
    </el-dialog>
</template>
<script>
    import request from '@/utils/request'
    import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'

    export default {
        components: {},
        props: [],
        data() {
            return {
                visible: false,
                loading: false,
                btnLoading: false,
                selectDialogVisible: false,
                currTableConf:{},
                addTableConf:{
            },
                tableRows:{
                },
            currVmodel:"",
            dataForm: {
                                                            code : "",
                                                                name : '',
                                                            enabledmark : "372347989370343493",
                                                            status : "-1",
                                                            attachment : [],
                                                            creatoruserid : "",
                                                            creatortime : "",
                                                            lastmodifyuserid : "",
                                                            lastmodifytime : "",
                                                                modifyreason : '',
                                                                description : '',
                                                                approvetime : '',
                                                                approver : '',
            },
            rules:
            {



            },
                        enabledmarkOptions:[],
                        statusOptions:[{"fullName":"自由","id":"-1"},{"fullName":"审批通过","id":"1"}],

        }
        },
        computed: {},
        watch: {},
        created() {
                    this.getenabledmarkOptions();
        },
        mounted() {},
        methods: {
                    getenabledmarkOptions() {
                        getDictionaryDataSelector('372347785317453893').then(res => {
                            this.enabledmarkOptions = res.data.list
                        })
                    },
            clearData(){
                               this.dataForm.code = "";
                               this.dataForm.name = '';
                               this.dataForm.enabledmark = "372347989370343493";
                               this.dataForm.status = "-1";
                                 this.dataForm.attachment = [];
                               this.dataForm.creatoruserid = "";
                               this.dataForm.creatortime = "";
                               this.dataForm.lastmodifyuserid = "";
                               this.dataForm.lastmodifytime = "";
                               this.dataForm.modifyreason = '';
                               this.dataForm.description = '';
                               this.dataForm.approvetime = '';
                               this.dataForm.approver = '';
            },
            init(id) {
                this.dataForm.id = id || 0;
                this.visible = true;
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if(this.dataForm.id){
                        this.loading = true
                        request({
                            url: '/api/example/Supplier/'+this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataInfo(res.data)
                            this.loading = false
                        });
                    }else{
                       this.clearData()
                    }
                });
                this.$store.commit('generator/UPDATE_RELATION_DATA', {})
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                            this.request()
                    }
                })
            },
            request() {
                this.btnLoading = true
                let _data =this.dataList()
                if (!this.dataForm.id) {
                    request({
                        url: '/api/example/Supplier',
                        method: 'post',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }else{
                    request({
                        url: '/api/example/Supplier/'+this.dataForm.id,
                        method: 'PUT',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }
            },
            openSelectDialog(key) {
                this.currTableConf=this.addTableConf[key]
                this.currVmodel=key
                this.selectDialogVisible = true
                this.$nextTick(() => {
                    this.$refs.selectDialog.init()
                })
            },
            addForSelect(data) {
                for (let i = 0; i < data.length; i++) {
                    let item={...this.tableRows[this.currVmodel],...data[i]}
                    this.dataForm[this.currVmodel].push(item)
                }
            },
            dataList(){
                var _data = JSON.parse(JSON.stringify(this.dataForm));
        _data.attachment = JSON.stringify(_data.attachment)
                return _data;
            },
            dataInfo(dataAll){
                let _dataAll =dataAll
        _dataAll.attachment = JSON.parse(_dataAll.attachment)
                this.dataForm = _dataAll
            },
        },
    }

</script>
